<template>
  <div class="user">
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/index' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <div class="left">
        <el-input
          v-model="users.query"
          class="ipt"
          clearable
          placeholder="请输入内容"
        >
        </el-input>
        <el-button class="el-icon-search" type="info" @click="getList()"></el-button>
        <el-button @click="tianjia('tianjia')">添加用户</el-button>
      </div>
      <el-dialog :visible.sync="dialogTableVisible" style="height: 600px" title="添加用户">
        <el-form v-if="changeType ==='bianji'" ref="form" :model="form" :rules="rules">
          <el-form-item :label-width="formLabelWidth" label="用户名" prop="username">
            <el-input v-model="form.username" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item :label-width="formLabelWidth" label="邮箱" prop="email">
            <el-input v-model="form.email" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item :label-width="formLabelWidth" label="手机" prop="mobile">
            <el-input v-model="form.mobile" autocomplete="off"></el-input>
          </el-form-item>
        </el-form>
        <el-form v-else-if="this.changeType ==='tianjia'" ref="form" :model="form" :rules="rules">
          <el-form-item :label-width="formLabelWidth" label="用户名" prop="username">
            <el-input v-model="form.username" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item :label-width="formLabelWidth" label="密码" prop="password">
            <el-input v-model="form.password" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item :label-width="formLabelWidth" label="邮箱" prop="email">
            <el-input v-model="form.email" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item :label-width="formLabelWidth" label="手机" prop="mobile">
            <el-input v-model="form.mobile" autocomplete="off"></el-input>
          </el-form-item>
        </el-form>
        <div v-else class="fenpeijiaose" style="width: 300px">
          <span>当前用户:</span><span>{{ a }}</span><br>
          <span>当前角色:</span><span>{{ b }}</span><br>
          <span>添加新角色</span><span></span><br>
          <el-select v-model="list.id" placeholder="请选择" @change="changeSelect">
            <el-option
              v-for="item in list"
              :key="item.id"
              :label="item.roleName"
              :value="item.id">
            </el-option>
          </el-select>
        </div>
        <div slot="footer" class="dialog-footer">
          <el-button @click="res">取 消</el-button>
          <el-button type="primary" @click="add">确 定</el-button>
        </div>
      </el-dialog>
      <el-table
        :data="tableData"
        border
        stripe
        style="width: 100%">
        <el-table-column
          type="index" width="30"
        >
        </el-table-column>
        <el-table-column
          label="姓名"
          prop="username"
          width="180">
        </el-table-column>
        <el-table-column
          label="邮箱"
          prop="email"
          width="180">
        </el-table-column>
        <el-table-column
          label="电话"
          prop="mobile">
        </el-table-column>
        <el-table-column
          label="角色"
          prop="role_name">
        </el-table-column>
        <el-table-column
          label="状态"
          prop="mg_state">
          <template #default="{ row }">
            <el-switch
              v-model="row.mg_state"
              active-color="#13ce66"
              inactive-color="#ff4949"
              @change="changeSwitch(row)">
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template #default="{ row }">
            <el-button circle icon="el-icon-edit" type="primary" @click="changeCaozuo(row.id,'bianji',row)"></el-button>
            <el-button circle icon="el-icon-delete" type="danger" @click="del(row.id)"></el-button>
            <el-button circle icon="el-icon-s-tools" type="danger"
                       @click="changeCaozuo(row.id,'fenpei',row)"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          :current-page.sync="users.pagenum"
          :page-size="users.pagesize"
          :page-sizes="[2,3,4]"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        >
        </el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script>
import {
  addUser,
  delUser,
  getOneUser,
  getUser,
  xiugaiOneUser,
  fenPeiUser,
  fenPeiUser2,
  xiugaiZhuangTai
} from '@/api/user'

export default {
  name: 'hm-user',
  data () {
    return {
      ipt1: '',
      a: '',
      b: '',
      value: '',
      id: '',
      tableData: [],
      total: 0,
      changeType: 'tianjia',
      users: {
        query: '',
        // 当前的页数
        pagenum: 1,
        // 当前显示多少条数据
        pagesize: 2
      },
      formLabelWidth: '120px',
      dialogTableVisible: false,
      dialogFormVisible: false,
      form: {
        username: '',
        password: '',
        email: '',
        mobile: ''
      },
      rules: {
        username: [
          {
            required: true,
            trigger: 'blur',
            message: '请输入用户名'
          },
          {
            min: 5,
            max: 10,
            trigger: 'blur'
          }
        ],
        password: [
          {
            required: true,
            trigger: 'blur',
            message: '请输入密码'
          }
        ],
        email: [
          {
            required: true,
            trigger: 'blur',
            message: '请输入正确的邮箱'
          }
        ],
        mobile: [
          {
            required: true,
            trigger: 'blur',
            message: '请输入正确的手机号'
          }
        ]
      },
      list: [],
      id2: '',
      data: []
    }
  },
  created () {
    this.getList()
  },
  methods: {
    // 分页
    // 监听page size 方框
    handleSizeChange (newsize) {
      console.log(newsize)
      this.users.pagesize = newsize
      this.getList()
    },
    // 监听current change 页码值
    handleCurrentChange (newcurrent) {
      this.users.pagenum = newcurrent
      this.getList()
    },
    async changeSwitch (row) {
      row.mg_state ? this.$message.success('更新状态成功') : this.$message.success('更新状态成功')
      await xiugaiZhuangTai(row.id, row.mg_state)
    },
    changeSelect (a) {
      this.value = a
    },
    tianjia (type) {
      this.changeType = type
      this.dialogTableVisible = true
      this.form.email = ''
      this.form.username = ''
      this.form.password = ''
      this.form.mobile = ''
    },
    async changeCaozuo (id, type, row) {
      this.changeType = type
      if (this.changeType === 'bianji') {
        this.id = id
        this.dialogTableVisible = true
        const res = await getOneUser(id)
        this.form = res.data
        console.log(id)
      }
      if (this.changeType === 'fenpei') {
        this.dialogTableVisible = true
        this.a = row.username
        this.b = row.role_name
        const res = await fenPeiUser()
        this.list = res.data
        this.id = id
      }
    },
    async del (id) {
      await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
      const res = await delUser(id)
      if (res.meta.status === 200) {
        this.$message.success(res.meta.msg)
      } else {
        this.$message.error(res.meta.msg)
      }
      this.getList()
    },
    async getList () {
      const res = await getUser(this.users)
      console.log(res)
      this.tableData = res.data.users
      this.total = res.data.total
      this.currentPage = res.data.pagenum
    },
    async add () {
      if (this.changeType === 'tianjia') {
        await this.$refs.form.validate()
        await addUser(this.form)
        this.dialogTableVisible = false
        this.$message.success('添加成功')
      }
      if (this.changeType === 'bianji') {
        // await this.$refs.form.resetFields()
        const res = await xiugaiOneUser(this.id, this.form)
        console.log(res)
        this.$message.success(res.meta.msg)
        this.dialogTableVisible = false
        this.getList()
      }
      if (this.changeType === 'fenpei') {
        await fenPeiUser2(this.id, this.value)
        this.getList()
        this.dialogTableVisible = false
      }
    },
    res () {
      this.form.email = ''
      this.form.username = ''
      this.form.password = ''
      this.form.mobile = ''
      this.dialogTableVisible = false
    }
  }
}
</script>

<style lang="scss" scoped>
.user {
  .el-card {
    margin-top: 20px;

    .fenpeijiaose {
      padding: 0;
      line-height: 50px;
      text-align: left;
    }

    .left {
      display: flex;
      justify-content: left;
      align-items: center;
      height: 40px;
      margin-bottom: 20px;

      .ipt {
        width: 410px;
      }

      .el-button {
        height: 40px;
      }
    }

    .el-pagination {
      padding-top: 15px;
      line-height: 100%;
      height: 35px;
    }
  }

  :deep.el-table__header {
    height: 20px;
  }
}

</style>
